from django.contrib.auth.backends import ModelBackend
from user.models import UserModel
from django.db.models import Q

def get_user_by_account(account):
    user = UserModel.objects.filter(Q(username=account) | Q(mobile=account) | Q(email=account)).first()
    return user

class CustomJwtModelBackends(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        if username is None:
            username = kwargs.get(UserModel.USERNAME_FIELD)
        if username is None or password is None:
            return
        user = get_user_by_account(username)
        if user and user.check_password(password) and self.user_can_authenticate(user):
            return user
